package com.rjwh.dingdong.client.activity.loginregist.fragment;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.aishua.lib.utils.AsStrUtils;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.fragment.BaseFragment;
import com.rjwh.dingdong.client.mvp.presenter.impl.MobileFastLoginPresenter;
import com.rjwh.dingdong.client.mvp.view.MobileFastLoginView;
import com.rjwh.dingdong.client.util.ContactPhoneUtils;
import com.rjwh.dingdong.client.util.RxBus;
import com.rjwh.dingdong.client.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.rjwh.dingdong.client.R.id.mobile_fast_login_get_code_btn;

/**
 * 手机号快捷登录
 *
 * @author Kay
 * @ClassName MobileFastLoginFragment
 * @Description
 * @date 2016-10-9
 * @history 2016-10-9 author: description:
 */

public class MobileFastLoginFragment extends BaseFragment implements MobileFastLoginView {


    @BindView(mobile_fast_login_get_code_btn)
    Button getCodeBtn;
    @BindView(R.id.mobile_fast_login_mobile_number)
    EditText phoneNoEt;
    @BindView(R.id.mobile_fast_login_verification_code)
    EditText codeInputEt;
    @BindView(R.id.mobile_fast_login_btn)
    Button loginBtn;
    private String currentPhone;
    private String codeInput;
    private TimeCount time;

    private MobileFastLoginPresenter presenter;

    public static MobileFastLoginFragment getInstance() {
        MobileFastLoginFragment fragment = new MobileFastLoginFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.mobile_fast_login_fragment_layout, null);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView() {
        getCodeBtn.setClickable(false);
        if (ContactPhoneUtils.isMobileNO(phoneNoEt.getText().toString()) && !AsStrUtils.isEmpty(codeInputEt.getText().toString())) {
            loginBtn.setBackgroundResource(R.drawable.long_btn);
            loginBtn.setClickable(true);
        } else {
            loginBtn.setBackgroundResource(R.drawable.long_btn_unable);
            loginBtn.setClickable(false);
        }
        phoneNoEt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                updateLoginBtnState();
                updateCodeBtnState();
            }
        });
        codeInputEt.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                updateLoginBtnState();
            }
        });
    }

    private void updateLoginBtnState() {
        if (!ContactPhoneUtils.isMobileNO(phoneNoEt.getText().toString()) || AsStrUtils.isEmpty(codeInputEt.getText().toString())) {
            loginBtn.setBackgroundResource(R.drawable.long_btn_unable);
            loginBtn.setClickable(false);
        } else {
            loginBtn.setBackgroundResource(R.drawable.long_btn);
            loginBtn.setClickable(true);
        }
    }


    private void updateCodeBtnState() {
        if (!ContactPhoneUtils.isMobileNO(phoneNoEt.getText().toString())) {
            getCodeBtn.setClickable(false);
            getCodeBtn.setBackgroundResource(R.drawable.gray_white_corner_normal);
            getCodeBtn.setTextColor(getContext().getResources().getColor(R.color.content_hint_text_color));
        } else {
            getCodeBtn.setBackgroundResource(R.drawable.gray_white_corner_pressed);
            getCodeBtn.setClickable(true);
            getCodeBtn.setTextColor(getContext().getResources().getColor(R.color.blue_button_nor));
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        presenter = new MobileFastLoginPresenter(getContext(), this);
        time = new TimeCount(10 * 6000, 1000);// 构造CountDownTimer对象
    }

    @OnClick({R.id.mobile_fast_login_btn, R.id.mobile_fast_login_user_agreement, mobile_fast_login_get_code_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.mobile_fast_login_get_code_btn:
                String inputPhone1 = phoneNoEt.getText().toString();
                currentPhone = inputPhone1;
                doGetCode(inputPhone1);
                break;
            case R.id.mobile_fast_login_btn:
                String inputPhone2 = phoneNoEt.getText().toString();
                codeInput = codeInputEt.getText().toString();
                if (AsStrUtils.isEmpty(inputPhone2) || AsStrUtils.isEmpty(codeInput)) {
                    ToastUtil.showToast(getContext(), "请先获取验证码！");
                    return;
                }
                doFastLogin(inputPhone2, codeInput);
                break;
            case R.id.mobile_fast_login_user_agreement:

                break;

        }
    }

    //快捷登录
    private void doFastLogin(String inputPhone2, String codeInput) {
        presenter.doSysSmsCodelogin(inputPhone2, codeInput);
    }

    //获取验证码
    private void doGetCode(String inputPhone1) {
        presenter.doGetCode(inputPhone1);
    }

    @Override
    public void showLoading() {
        showLoadDialog();
    }

    @Override
    public void dismissLoading() {
        dismissLoadDialog();
    }

    @Override
    public void LoadData() {
        RxBus.get().post(LocalConstant.RX__POST_LOGIN_UPDATE_UI, 1);
        RxBus.get().post(LocalConstant.RX__POST_WEB_FETCHINFO, 1);
        getActivity().finish();
    }

    @Override
    public void setTimerData() {
        time.start();
    }

    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {// 计时完毕时触发
            getCodeBtn.setText("获取验证码");
            updateCodeBtnState();
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程显示
            getCodeBtn.setClickable(false);
            getCodeBtn.setBackgroundResource(R.drawable.gray_white_corner_normal);
            getCodeBtn.setText(millisUntilFinished / 1000 + "秒后重试");
            getCodeBtn.setTextColor(getContext().getResources().getColor(R.color.content_hint_text_color));
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        time.cancel();
        presenter.detachView();
    }
}
